var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.paytypelist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list);
        });
    },
    getDataList: function(data){
        var _this = this;

        if(data && data.length > 0){
          var i = 0; len = data.length, html = '';
          for (i;i<len;i++) {
            html += '<tr>'+
              '<td class="text-c">'+ (data[i].name || '/') +'</td>'+
              '<td class="text-c">'+ data[i].describe +'</td>';
            if(data[i].isUse === 0){
              html += '<td class="text-c"><span class="switch switch-off switch-item" themeColor="#3498db" data-val="0" data-id="'+data[i].payId+'"></span></td>';
            } else {
              html += '<td class="text-c"><span class="switch switch-on switch-item" themeColor="#3498db" data-val="1" data-id="'+data[i].payId+'"></span></td>';
            }
              html += '<td class="text-c"><input type="text" class="input-text text-c sort" value="'+data[i].sort+'" data-id="'+data[i].payId+'"></td>'+
              '<td class="text-c">'+
              '<a class="handle edit" data-id="'+data[i].payId+'" >编辑</a>'+
              '</td>'+
              '</tr>';
          }

          $('#List').html(html);

          _this.getDataEvent();
        } else {
          $('#List').html('<tr><td colspan="5" class="text-c">暂无数据</td></tr>');
        }
    },
    getDataEvent:function(){
        var _this = this;
        honeySwitch.init();
        _this.switchChange();
        $('.sort').on('blur', function(){
            var thisId = $(this).attr('data-id'),
              thisVal = $(this).val();
            _this.sortEvent(thisId, thisVal);
        });
        $('.edit').on('click', function() {
            Pub.setSession('payId', $(this).attr('data-id'));
            window.location.href = Pub.getHtmlUrl('./payment-set-edit.html');
        });
    },
    isEnabled: function(id, val){
      var _this = this;
      var Params = {
        passport: _this.params.passport,
        payId: id,
        isUse: val
      };
      console.log(Params)
      Api.paytypelistuse(Params);
    },
    sortEvent: function(id, val){
        var _this = this;
        var Params = {
          passport: _this.params.passport,
          payId: id,
          sort: val
        };
        Api.paytypelistsort(Params, function(data){
          console.log(data);
        })
    },
    switchChange: function(){
        var _this = this;
        switchEvent(".switch-item",function(){
          $('.switch-item').attr('data-val', 1);
        },function(){
          $('.switch-item').attr('data-val', 0);
        });
        $('.switch-item').on('click', function(){
          var thisId = $(this).attr('data-id'),
            thisVal = $(this).attr('data-val');
          _this.isEnabled(thisId, thisVal);
        });
    },
    otherEvent:function(){
        var _this = this;

        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
    },
    initEvent: function(){
        var _this = this;
         _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();